package com.ruoyi.flowable.common.expand.el;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 扩展表达式
 *
 * @author Tony
 * @date 2023-03-04 12:10
 */
@Component
@Slf4j
public class FlowEl implements BaseEl {

    @Resource
    private ISysDeptService sysDeptService;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ISysRoleService sysRoleService;

    //根据userId获取指定部门成员
    public String findUserByUserId(String userId){
        log.info("userId:{}", userId);
        return sysUserService.selectUserById(Long.valueOf(userId)).getUserId().toString();
    }

    //根据deptId、角色获取指定部门成员
    public List<String> findUserByDeptId(String deptId,String roleName){
        log.info("deptId:{},roleName:{}", deptId, roleName);
        return sysUserService.selectUserByDeptIdAndRoleName(Long.valueOf(deptId), roleName);
    }

    //根据deptName、角色获取指定部门成员
    public List<String> findUser(String deptName,String roleName){
        log.info("deptName:{},roleName:{}", deptName, roleName);
        return sysUserService.selectUserByDeptNameAndRoleName(deptName, roleName);
    }

    //根据deptId获取所有部门成员 移动-维修1
    public List<String> findDeptUserByDeptId(Integer deptId){
        log.info("deptId:{}", deptId);
        SysDept sysDept = sysDeptService.selectDeptById(Long.valueOf(deptId.toString()));
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(sysDept.getDeptId());
        List<SysUser> userList = sysUserService.selectUserListNoPage(sysUser);
        return userList.stream().map(item -> item.getUserId().toString()).collect(Collectors.toList());
    }

    //根据deptName获取所有部门成员
    public List<String> findDeptUser(String deptName){
        log.info("deptName:{}", deptName);
        SysDept sysDept = sysDeptService.selectDeptByName(deptName);
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(sysDept.getDeptId());
        List<SysUser> userList = sysUserService.selectUserListNoPage(sysUser);
        return userList.stream().map(item -> item.getUserId().toString()).collect(Collectors.toList());
    }

    public String findDeptLeader(String name){
        log.info("开始查询表达式变量值,getName");
        return name;
    }

    public String getName(String name){
        log.info("开始查询表达式变量值,getName");
        return name;
    }
}

